﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Phone.Management.Deployment;
using Windows.ApplicationModel;
using CompanyHub.Utilities;

namespace CompanyHub.Models
{
    public enum CompanyPackageStatus
    {
        Unknown,
        New,
        Update,
        Installed
    };

    public class CompanyPackage
    {
        public int Id { get; set; }

        #region Properties pulled from app package
        // Unique Guid to identify package
        public Guid PackageId { get; set; }

        // Name of app
        public string Title { get; set; }

        public string Author { get; set; }

        public string Publisher { get; set; }

        public string EnterpriseId { get; set; }

        public Guid PublisherId { get; set; }

        // String representation of current version
        public string Version { get; set; }
        #endregion

        // Description of app
        public string Description { get; set; }

        // Identifies which app is the company hub app. Should only be one
        public bool IsCompanyHub { get; set; }

        #region Properties managed by system

        public string Thumbnail { get; set; }

        public string SourceUri { get; set; }

        public bool IsActive { get; set; }

        public bool IsHistory { get; set; }

        public DateTime UpdatedDT { get; set; }
        #endregion

        public CompanyPackageStatus Status { get; set; }

        public static IEnumerable<CompanyPackage> GenerateData()
        {
            var data = new List<CompanyPackage>();

            // Get list of installed apps
            var pkgs = InstallationManager.FindPackagesForCurrentPublisher();

            foreach (var pkg in pkgs)
            {
                // Don't add hub app to the list
                if (pkg.Id.Name != "CompanyHub")
                {
                    // Create new CompanyPackage obj using info from Id

                    var compPkg = new CompanyPackage()
                    {
                        PackageId = Guid.Parse(pkg.Id.ProductId),
                        Title = pkg.Id.Name,
                        Description = "Test Description",
                        Version = PackageVersionHelper.VersionToString(pkg.Id.Version),
                        Thumbnail = "/Assets/ApplicationIcon.png",
                        Status = CompanyPackageStatus.Unknown
                    };

                    // Create some updatable versions
                    if (data.Count() > 3)
                    {
                        compPkg.Version = "2.0.0.0";
                    }

                    data.Add(compPkg);
                }
            }

            // Add three new products
            for (int i = 7; i < 10; i++)
            {
                var compPkg = new CompanyPackage()
                {
                    PackageId = Guid.NewGuid(),
                    Title = "CDS APP " + i,
                    Description = "Test Description",
                    Version = "1.0.0.0",
                    Thumbnail = "/Assets/ApplicationIcon.png",
                    Status = CompanyPackageStatus.Unknown
                };

                data.Add(compPkg);
            }

            return data;
        }
    }
}
